package com.todo.event.activity.application.handler.fieldtransfer;

import com.todo.event.activity.application.core.engine.aviator.AviatorContext;
import com.todo.event.activity.application.core.engine.aviator.AviatorHelper;
import com.todo.event.activity.application.entity.context.FieldTransferContext;
import com.todo.event.activity.common.enums.EventFieldDataSourceEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author azhebuxing
 * @date 2025/1/9 19:37
 * @description 基础字段逻辑处理
 */
@Slf4j
@Component
public class AviatorHandler extends AbstractFieldTransferHandle {
    @Override
    protected String doTransfer(FieldTransferContext context) {
        AviatorContext aviatorContext = new AviatorContext();
        aviatorContext.setEnv(context.getPayload());
        aviatorContext.setExpression(context.getTransfer());
        aviatorContext.setCached(true);
        Object execute = AviatorHelper.execute(aviatorContext);
        return execute == null ? null : String.valueOf(execute);
    }

    @Override
    protected Boolean doMatch(FieldTransferContext context) {
        boolean validated = AviatorHelper.validateExp(context.getTransfer());
        return validated;
    }

    @Override
    protected EventFieldDataSourceEnum getHandleType() {
        return EventFieldDataSourceEnum.AVIATOR;
    }
}
